#!/bin/bash
set -e

CURRENT_VERSION_CHROMIUM=`docker container run --rm --entrypoint "" zenika/alpine-chrome:with-puppeteer-xvfb chromium-browser --version`
CHROMIUM_VERSION_REGEXP='Chromium ([0-9]+)\.'
if [[ $CURRENT_VERSION_CHROMIUM =~ $CHROMIUM_VERSION_REGEXP ]]; 
then echo "ℹ Succesfully extracted Chromium major version of ${BASH_REMATCH[1]} from $CURRENT_VERSION_CHROMIUM"; 
else echo "❌ Cannot extract Chromium major version from $CURRENT_VERSION_CHROMIUM"; 
fi
CHROMIUM_MAJOR_VERSION=${BASH_REMATCH[1]}
echo $CHROMIUM_MAJOR_VERSION

for tag in $CHROMIUM_MAJOR_VERSION; do
    echo $tag        
    tagStart=$(expr index "$IMAGE_NAME" :)
    repoName=${IMAGE_NAME:0:tagStart-1}
    docker tag $IMAGE_NAME ${repoName}:${tag}-with-puppeteer-xvfb
    docker push ${repoName}:${tag}-with-puppeteer-xvfb
done
